﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Reflection;
using Metaspec;

// Documentation comments example (see Annex E in standard).

namespace DocumentationCommentsExample
{
	class Program
	{
		static void Main(string[] args)
		{
			//creating project
			ICsProject project = ICsProjectFactory.create(project_namespace.pn_project_namespace);
			project.setBuildEntityModel(true);
			project.setParseDocumentationComments(true);

			//adding C# file
			string directory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..");
			string path = Path.Combine(directory, "Test.cs");
			char[] buffer = File.ReadAllText(path).ToCharArray();
			ICsFile file = ICsFileFactory.create(buffer, path);
			project.addFiles(file);

			//parsing
			project.parse(true, false);

			{
				CsCompilationUnit unit = project.getFiles()[0].getCompilationUnit();
				CsNamespace ns = (CsNamespace)unit.declarations[0];
				CsClass c = (CsClass)ns.member_declarations[0];
				CsMethod m = (CsMethod)c.member_declarations[0];

				//checking cref tag
				CsEntityMethod method_g = (CsEntityMethod)getEntity(project.getNamespace(), "Test", "C", "g");
				DcElement e1 = m.dc.dcelements[0];
				DcElement ee1 = (DcElement)e1.content.content[1];
				DcAttribute a = ee1.tag.attributes[0];
				Debug.Assert(a.entity_data.entity == method_g);

				//checking param tag
				CsEntityMethod method_f = (CsEntityMethod)getEntity(project.getNamespace(), "Test", "C", "f");
				DcAttribute a2 = m.dc.dcelements[1].tag.attributes[0];
				Debug.Assert(a2.entity_data.entity == method_f.parameters[0]);
			}
		}

		static CsEntity getEntity(CsScopeEntity scope, params string[] names)
		{
			CsEntity entity = null;
			for (int i = 0; i != names.Length; ++i)
			{
				entity = scope.scope_table.get_single_id(names[i]);
				if (i != names.Length - 1)
				{
					scope = (CsScopeEntity)entity;
				}
			}
			return entity;
		}
	}
}
